package com.zj.core.dialog

import android.app.Activity
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import com.fileutils.BaseConstant
import com.fileutils.NumberUtils
import com.jniutils.DataCollUtil
import com.systemutils.SoftKeyboardUtils
import com.zj.core.R
import com.zj.core.base.BaseDialog
import com.zj.core.interfaces.TextMessageIml
import com.zj.core.tools.ViewPosition
import com.zj.core.tools.VoiceAsrTools
import com.zj.core.ui.NoteDialogUi
import java.lang.StringBuilder

class NoteDialog(activity: Activity?, title:String?, noteIml: TextMessageIml?) : BaseDialog(activity), View.OnClickListener,
    TextMessageIml {
    private var mActivity:Activity?=activity
    private var mCallIml: TextMessageIml?=noteIml
    private var noteDialogView: NoteDialogUi?=null
    private var mTitle:String?=title
    private var mBuilder:StringBuilder?=null
    private var mHandle:VoiceAsrTools?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val width: Int = ViewPosition().getScreenWidth(this.context)
        val height: Int = (ViewPosition().getScreenHeight(this.context) * 0.95).toInt()
        val params = LinearLayout.LayoutParams(width, height, 0f)
        noteDialogView= NoteDialogUi(context)
        noteDialogView?.apply {
            setContentView(common_dialog_root_view,params)
            mBuilder= StringBuilder()
            //setCancelable(cancelable)
            setCanceledOnTouchOutside(true)
            common_dialog_cancel_tv?.setOnClickListener(this@NoteDialog)
            common_dialog_confirm_tv?.setOnClickListener(this@NoteDialog)
            common_dialog_root_view?.setOnClickListener(this@NoteDialog)
            common_dialog_message_tv?.setText(mTitle)
            common_dialog_message_tv?.setSelection(common_dialog_message_tv?.text.toString().length)
            guide_line_lly?.setOnClickListener(this@NoteDialog)
        }
        mHandle=VoiceAsrTools().setVoiceImage(noteDialogView?.voice_img_view).setHandleListener(this)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        mHandle?.onKeyDown(keyCode,mActivity,noteDialogView?.common_dialog_message_tv?.windowToken)
        return true
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
        mHandle?.onKeyUp(keyCode,mActivity)
        return false
    }

    override fun messageResult(msg: String) {
        specificVoice(noteDialogView?.common_dialog_message_tv?.text.toString()+" "+msg).apply {
            noteDialogView?.common_dialog_message_tv?.setText(this)
            noteDialogView?.common_dialog_message_tv?.setSelection(this.length)
        }
    }

    /**
     * 床号匹配
     */
    private fun specificVoice(str:String):String{
        if(str=="起床"){
            return "7床"
        }

        DataCollUtil.patternValue(BaseConstant.PATTERN_BED,str)?.let {
            mBuilder?.clear()
            if(it!=""&&it!="床"){
                val index=str.indexOf(it)
                mBuilder?.append(str.replace(it,""))
                return mBuilder?.insert(index, NumberUtils.CharDateFormat(it)).toString()
            }
        }
        return str
    }

    override fun onClick(v: View?){
        when (v?.id) {
            R.id.common_dialog_cancel_tv -> dismiss()

            R.id.common_dialog_confirm_tv ->{
                mCallIml?.messageResult(noteDialogView?.common_dialog_message_tv?.text.toString())
                dismiss()
            }

            R.id.parent_cs_layout->{
                dismiss()
            }

            R.id.guide_line_lly->{}
        }
    }
}